package com.zyk.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangsan
 * @date 2021/4/25 22:45
 */
public class C17 {

    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<String>();
        if (digits.length() == 0) {
            return combinations;
        }
        Map<Character, String> phoneMap = new HashMap<>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        return combinations;
    }

    private void backtrack(List<String> ans, Map<Character, String> phoneMap, String digits, int i, StringBuffer sb) {
        if(i == digits.length()) {
            ans.add(sb.toString());
            return;
        }
        char digit = digits.charAt(i);
        String letters = phoneMap.get(digit);
        int lettersCount = letters.length();
        for (int j = 0; j < lettersCount; j++) {
            sb.append(letters.charAt(j));
            backtrack(ans, phoneMap, digits, i + 1, sb);
            sb.deleteCharAt(i);
        }
    }


    public static void main(String[] args) {
        String digits = "23";
        System.out.println(new C17().letterCombinations(digits));
    }

}
